/**
\author Zachary Wartell - Copyright 2006: All rights reserved.
\brief Implementation of linked list class
*/
#include "List.h"
#include <iostream>

using namespace std;


/**
\brief construct a Node
*/
List::Node::Node (const char name0[])
    {
    strncpy(name,name0,NAME_SIZE);
    next = NULL;
    }

/**
\brief destruct a Node.

Because List::Node itself contains no dynamically allocated data, there really isn't any need to define this 
destructor but I put it here for the tutorial on debugging.
*/
List::Node::~Node()
    {
    return;
    }

/**
\brief print all nodes
*/
void List::Node::printAll()const
    {
    cout << name << endl;
    if (next!=NULL)
	next->printAll();
    }

/**
\brief traverse all nodes
*/
void List::Node::traverseAll(Node::Process pn)
    {
    pn(*this);
    if (next!=NULL)
	next->traverseAll(pn);
    }

/**
\brief construct a List
*/
List::List()
    {
    head = NULL;
    }

/**
\brief destruct a List by destruct'ing all the nodes
*/
List::~List()
    {
    Node* n;
    for(n=head;n!=NULL;)
	{
	Node* next=n->next; // save 'next' before destroying 'n'
	//delete n;
	n = next;
	}
    }

/**
\brief add a new Node to this List
*/
void List::add(const char name[])
    {
    if (head==NULL)
	head = new Node(name);
    else
	{
	Node *n;
	n=head;
	for(n=head;true;n=n->next)
	    {
	    if (n->next == NULL)
		{
		n->next = new Node(name);
		break;
		}
	    }
	}    
    }

/**
\brief Print this List
*/
void List::print() const
    {
    if (head!=NULL)
	head->printAll();
    }

/**
\brief Traverse this List call function 'pn' on each node
*/
void List::traverse(Node::Process pn)
    {
    if (head!=NULL)
	head->traverseAll(pn);
    }